let login_id;
$(document).ready(function () {
    if (isLogin() == undefined) {
        alert("你还没有登录，去登陆吧");
        location.href = "login.html";
    }
    login_id = get_login(isLogin());
    
    compute_order();
})
//计算订单
function compute_order(){
    //订单状态 0待付款 1待发货 2 待收货 3 待评价 4 已完成 5已取消 6已退货 7已退款
    let order_num = 0;
    let start_1 = 0;
    let orders = $(".orders");
    get_order(login_id).forEach((e, i) => {
        order_num++;
        let d = new Date(parseInt(e.create_time));
        let table = $("<table class='sui-table table-bordered'>");
        let tbody = $("<tbody>");
        let thead = $(`<thead><tr><th colspan="5">
                    <span class="ordertitle">${d.getFullYear()}-${d.getMonth()}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}　订单编号：${e.order_sn} 
                    <span class="pull-right delete"><img src="../img/delete.png"></span></span>
                    </th>
                </tr>
            </thead>`);
        //获取该订单的商品
        let order_goods = get_order_goods(e.id);
        if(e.order_status=="1"){
            start_1++;
        }
        order_goods.forEach((good, index) => {
            let tr ;
            if (index == 0) {
                tr = $(`<tr>
                    <td width="60%">
                        <div class="typographic"><img src="${good.goods_logo}">
                            <a href="#" class="block-text">${good.goods_name}</a><span>x${good.number}</span>
                            <ul class="unstyled list-inline">
                                <li>申请售后</li>
                            </ul>
                        </div>
                    </td>
                    <td rowspan="${order_goods.length}" width="8%" class="center">${e.consignee}</td>
                    <td rowspan="${order_goods.length}" width="13%" class="center">
                        <ul class="unstyled">
                            <li>总金额¥${e.total_amount}</li>
                            <li>在线支付</li>

                        </ul>
                    </td>
                    <td rowspan="${order_goods.length}" width="8%" class="center">
                        <a href="#" class="btn">已完成 </a>
                    </td>
                    <td rowspan="${order_goods.length}" width="13%" class="center">
                        <ul class="unstyled">
                            <li>
                                <a href="mycomment.html" target="_blank">评价|晒单</a>
                            </li>

                        </ul>
                    </td>
                </tr>`);
            }else{
                tr = $(`<tr>
                <td width="50%">
                    <div class="typographic"><img src="${good.goods_logo}">
                        <a href="#" class="block-text">${good.goods_name}</a><span>x${good.number}</span>
                        <ul class="unstyled">
                            <li>申请售后</li>
                        </ul>
                    </div>
                </td>
            </tr>`)
            }
            tbody.append(tr);
        })
        table.append(thead);
        table.append(tbody);
        orders.append(table);
    })
    $(".nav-tabs li:eq(0) i").html(order_num);
    $(".nav-tabs li:eq(1) i").html(start_1);
}